package com.xzq.bugstack.server;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @Author xzq
 * @Description //TODO
 * @Date 2021/11/20 11:05
 * @Version 1.0.0
 **/
public class MyServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
        String msg = packet.content().toString(Charset.forName("gbk"));
        System.out.println(new SimpleDateFormat("yyyy-MM-dd Hh:mm:ss").format(new Date()) + " : "+msg);

        //向客户端发送消息
        String str = "hello,我已经收到了你累消息\r\n";
        byte[] bytes = str.getBytes(Charset.forName("gbk"));
        DatagramPacket data = new DatagramPacket(Unpooled.copiedBuffer(bytes), packet.sender());
        ctx.writeAndFlush(data);
    }
}
